/*
 * 文件名：SupplierAction.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：wuliquan
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.consumables.supplier.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.wootide.cms.common.Constants;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.common.util.CommonUtil;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.consumables.supplier.pojo.SupplierInfo;
import com.wootide.cms.service.consumables.supplier.service.SupplierInfoService;
import com.wootide.cms.sys.common.action.BaseAction;

/**
 * 供应商Action层控制跳转
 * 〈功能详细描述〉
 * @author wuliquan
 * @version 2013-2-22
 * @see SupplierInfoAction
 * @since
 */
public class SupplierInfoAction extends BaseAction implements ServletRequestAware
{

    /**
     * 意义，目的和功能，以及被用到的地方<br>
     */
    private static final long serialVersionUID = 1L;
    
    /**
     * 供应商信息
     */
    private SupplierInfo supplierInfo;
    
    /**
     * 日志信息
     */
    private RunLog runLog;
    
    /**
     * 供应商业务层实例
     */
    private SupplierInfoService supplierInfoService;
    
    /**
     * 分页信息
     */
    private Page page;
    
    /**
     * 查询条件
     */
    private QueryCondition queryConditon;
    
    /**
     * HTTP请求对象
     */
    private HttpServletRequest request;
    
    /**
     * 结果集
     */
    private List<SupplierInfo> results;
    
    /**
     * 
     */
    private String message;
    
    /**
     * 是否操作成功
     */
    private Integer actResult = Constants.ACTRESULT_DEF;
    
    /**
     * 供应商ID
     */
    private Integer innerIndex;
    
    /**
     * 供应商集合
     */
    private Integer[] innerIndexs;
    
    /**
     * Description: 分页查询<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String doQuery()
    {
        try
        {
            // 如果从菜单进入，即在没有分页信息的情况下，则默认进入首页
            if (null == page)
            {
                page = new Page();
                page.setCurPage(1);
            }

            // 调用而业务层进行分页查询
            Result result = supplierInfoService.queryForPages(page,
                    queryConditon);

            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                QueryResults queryResults = (QueryResults) result
                        .getResultPojo();
                // 获取服务代码结果集
                runLog.debug(queryResults);
                results = queryResults.getResults();
                // 获取分页信息
                page = queryResults.getPage();

                // 跳转到分页页面
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }

        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 预添加<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String toAdd()
    {
        try
        {
            return SUCCESS;
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 添加供应商信息<br>
     * Implement: <br>
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String doAdd()
    {
        try
        {
            runLog.debug("supplierInfo=" + supplierInfo);

            // 调用业务层进行前转MO路由新增操作
            Result result = supplierInfoService.addOneSupplier(supplierInfo, request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 预修改供应商信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String toMod()
    {
        try
        {
            // 调用业务层获取一个路由对象
            Result result = supplierInfoService.viewOneSupplier(innerIndex);
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                supplierInfo = (SupplierInfo)result.getResultPojo();
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }

    /**
     * Description: 修改预订方式信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String 跳转标识
     * @author wuliquan
     */
    public String doMod()
    {
        try
        {
            // 调用业务层进行路由的修改操作
            Result result = supplierInfoService.modOneSupplier(supplierInfo, request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
       
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 删除供应商信息<br>
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author wuliquan
     */
    public String doDel()
    {
        try
        {
            runLog.debug("doDel");
            // 调用业务层进行删除操作
            Result result = supplierInfoService.delSuppliers(innerIndexs, request);
            int resultCode = result.getResultCode();

            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            // 如果删除中出现失败
            else
            {
                // 取得删除失败的条数
                int delErrorNum = result.getDelErrorNum();
                // 根据总条数和失败条数得出成功的条数
                int delSuccNum = innerIndexs.length - delErrorNum;
                // 定义国际化消息的参数
                String[] params = new String[]
                {
                    String.valueOf(delSuccNum), String.valueOf(delErrorNum)
                };
                message = getText(Constants.MESSAGE_PREFIX + resultCode,
                        params);
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 新增Ajax校验<br>
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author wuliquan
     */
    public String doCheckWhenAdd()
    {
        Result result = supplierInfoService.checkContentWhenAdd(supplierInfo.getSupplierName());
        int resultCode = result.getResultCode();
        message = CommonUtil.getExistCheckMessage(resultCode,
                getText("VALIDATOR.USER.USERNAME"),
                getText("VALIDATOR.USER.USERNAME.PASS"));            
        return SUCCESS;
    }
    
    /**
     * 获取系统HttpServletRequest实例
     */
    public void setServletRequest(HttpServletRequest request)
    {
        
        this.request = request;
    }

    public SupplierInfo getSupplierInfo()
    {
        return supplierInfo;
    }

    public void setSupplierInfo(SupplierInfo supplierInfo)
    {
        this.supplierInfo = supplierInfo;
    }

    public RunLog getRunLog()
    {
        return runLog;
    }

    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }

    public Page getPage()
    {
        return page;
    }

    public void setPage(Page page)
    {
        this.page = page;
    }

    public QueryCondition getQueryConditon()
    {
        return queryConditon;
    }

    public void setQueryConditon(QueryCondition queryConditon)
    {
        this.queryConditon = queryConditon;
    }

    public List<SupplierInfo> getResults()
    {
        return results;
    }

    public void setResults(List<SupplierInfo> results)
    {
        this.results = results;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public void setSupplierInfoService(SupplierInfoService supplierInfoService)
    {
        this.supplierInfoService = supplierInfoService;
    }

    public HttpServletRequest getRequest()
    {
        return request;
    }

    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }

    public Integer getActResult()
    {
        return actResult;
    }

    public void setActResult(Integer actResult)
    {
        this.actResult = actResult;
    }

    public Integer getInnerIndex()
    {
        return innerIndex;
    }

    public void setInnerIndex(Integer innerIndex)
    {
        this.innerIndex = innerIndex;
    }

    public Integer[] getInnerIndexs()
    {
        return innerIndexs;
    }

    public void setInnerIndexs(Integer[] innerIndexs)
    {
        this.innerIndexs = innerIndexs;
    }
    
    
    
}
